<?php
namespace App\Controller;
use Think\Controller;
use App\Controller\BaseController;
class ExaminerController extends BaseController {

    /**
     * 测试数据
     * @author xuan
     * @param id midiID
     */
    public function test(){
        $data['user_id'] = isset($_GET['id'])?$_GET['id']:1;
        $user = M('user')->where($data)->find();
        $this->backToClient(1,'测试数据',$user);
    }

    /**
     * 1 考官登录
     * @author xuan
     * @param user_id       考官电话号码或者midiID
     * @param user_password 考官密码
     */
    public function login(){
        $json_arr = $this->Security(false);

        //版本控制（强制更新）  1.未传版本号   2.版本号小于最新版本号
        $examiner_version = M('version')->where('version_client = 2')->order('version_code desc')->find();
        if (empty($json_arr['examiner_version'])) {
            $this->backToClient(300,'请更新到最新版本');
        }elseif ($json_arr['examiner_version'] < $examiner_version['version_code']) {
            $this->backToClient(300,'请更新到最新版本');
        }

        // $json_arr['user_id'] = 6;
        // $json_arr['user_password'] = 111111;
        if($json_arr['user_id'] > 10000000){
            $json_arr['user_phone'] = $json_arr['user_id'];
            unset($json_arr['user_id']);
        }
        $json_arr['user_password'] = md5($json_arr['user_password']);
        if($user_arr = M('user')->where($json_arr)->find()){
            $examiner['user_id'] = $user_arr['user_id'];
            $examiner['user_role'] = array('IN','3,4');
            if(empty(M('user')->where($examiner)->find())){
                $this->backToClient(202,'该账号不是考官！');
            }
            if (empty($user_arr['user_sig'])) {
                $data['user_sig'] = $this->getUserSig($user_arr['user_phone']);
                //usersig过期时间是生成以后的150天
                $data['user_sigexpire'] = time() + 60*60*24*150;
            }
            $data['user_last_time'] = date('Y-m-d H:i:s',time());
            $data['user_id'] = $user_arr['user_id'];
            $data['user_token'] = createRandom();
            M('user')->save($data);
            $user = M('user')->where('user_id = '.$user_arr['user_id'])->find();
            unset($user['user_password']);
            $this->backToClient(1,'登录成功',$user);
        }else{
            $this->backToClient(201,'账号或密码错误');
        }
    }

    /**
     * 2 修改考官个人信息
     * @author xuan
     * @param user_id               midiID（必传）
     * @param user_name             姓名（选填）
     * @param user_email            邮箱（选填）
     * @param user_phone            手机号码（选填）
     * @param user_avatar       真实头像（选填）
     * @param user_weixin           微信号（选填）
     */
    public function editUserinfo(){
        $json_arr = $this->Security();
        //要修改的手机号码不能是被注册过的号码
        $data['user_phone'] = $json_arr['user_phone'];
        $data['user_id'] = array('NEQ',$json_arr['user_id']);
        $user = M('user')->where($data)->find();
        if (!empty($user)) {
            $this->backToClient(103,'该手机号码已被注册');
        }
        M('user')->save($json_arr);
        $this->backToClient(1,'个人资料已更新');
    }

    /**
     * 3 修改密码
     * @author xuan
     * @param user_id               用户id（必填）
     * @param user_password         密码（必填）
     * @param user_newpassword      新密码（必填）
     */
    public function updatePassword(){
        $json_arr = $this->Security();
        $data['user_id'] = $json_arr['user_id'];
        $user = M('user')->field('user_id,user_password')->where($data)->find();
        if (empty($user)) {
            $this->backToClient(2,'用户不存在');
        }else{
            if (md5($json_arr['user_password']) == $user['user_password']) {
                $user['user_password'] = md5($json_arr['user_newpassword']);
                if (M('user')->save($user)) {
                    $this->backToClient(1,'修改密码成功');
                }else{
                    $this->backToClient(4,'新密码和旧密码重复');
                }
            }else{
                $this->backToClient(3,'原始密码错误');
            }
        }

    }

    /**
     * 4 重置密码
     * @author xuan
     * @param user_phone    手机号码（必填）
     * @param user_password 密码（必填）
     */
    public function resetPassword(){
        $json_arr = $this->Security();
        $data['user_phone'] = $json_arr['user_phone'];
        $user = M('user')->field('user_id')->where($data)->find();
        if (empty($user)) {
            $this->backToClient(104,'该手机号码未被注册');
        }
        $user['user_password'] = md5($json_arr['user_password']);
        M('user')->save($user);
        $this->backToClient(1,'重置密码成功！');

    }

    /**
     * 5 意见反馈
     * @author xuan
     * @param suggest_user_id       用户midiID（必填）
     * @param suggest_type          类型 1信息举报 2完善建议 3相关咨询 4考级反馈 5其他问题（必填）
     * @param suggest_content       反馈信息（必填）
     */
    public function suggest(){
        $json_arr = $this->Security();
        $json_arr['suggest_time'] = date('Y-m-d H:i:s',time());
        if(M('suggest')->add($json_arr)){
            $this->backToClient(1,'意见反馈发表成功');
        }else{
            $this->backToClient(121,'意见反馈发表失败！');
        }
    }

    /**
     * 6 消息列表(分页)
     * @author xuan
     * @param message_user_id       midiID（必填）
     */
    public function messageList(){
        $json_arr = $this->Security();
        // $json_arr['message_user_id'] = 77;
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        if (empty($json_arr['message_user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $reg_time = M('user')->where('user_id = '.$json_arr['message_user_id'])->getField('user_reg_time');
        $time = strtotime($reg_time);
        //考生端或者系统消息
        $data['message_display'] = 1;
        $data['message_client'] = 2;
        $data['message_time'] = array('GT',$time);
        $message_arr = M('message')->where($data)->where('message_user_id = '.$json_arr['message_user_id'].' or message_type = -1')->page($p,$pnum)->order('message_id desc')->select();
        $this->backToClient(1,'获取消息列表成功！',$message_arr);
    }

    /**
     * 7 上传文件接口
     * @author xuan
     */
    public function upload(){
        $json_arr = $this->Security();
        $finfo = upload($_FILES['file']);
        if ($finfo['status'] == 0) {
            $this->backToClient(4,$finfo['msg']);
        }else{
            $this->backToClient(1,'上传成功！',$finfo['filepath']);
        }
    }

    /**
     * 8 首页考试日程(七天)
     * @author xuan
     * @param user_id               考官ID（必填）
     */
    public function examSchedule(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 78;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $schedule = array();
        $data['exam_examiner_id'] = $json_arr['user_id'];
        $data['exam_midiclub_id'] = -1;
        $data['exam_paystatus'] = 2;
        $data['exam_status'] = 1;
        for ($i=0; $i < 7; $i++) {
            $data['exam_ymd'] = date('Y-m-d',strtotime('+'.$i.' days'));
            $schedule[] = M('exam')->alias('e')->field('exam_id,user_id,user_phone,user_avatar,user_name,user_sex,exam_major_id,exam_level,exam_ymd,examtime_time,user_idpic1')->join('left join midi_user as u on u.user_id = e.exam_user_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->where($data)->order('exam_time')->select();
        }
        $schedule = array_filter($schedule);
        $schedule = array_values($schedule);
        $this->backToClient(1,'获取考试日程(七天)成功！',$schedule);
    }

    /**
     * 9.查询证书
     * @author xuan
     * @param user_id           证书所有者（必填）
     * @param major_id          专业id（必填）
     * @param level             专业等级（必填）
     */
    public function certificateSearch(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1;
        // $json_arr['major_id'] = 1;
        // $json_arr['level'] = 7;
        $data['certificate_type'] = 1;
        $data['certificate_user_id'] = $json_arr['user_id'];
        $data['certificate_major_id'] = $json_arr['major_id'];
        $data['certificate_level'] = $json_arr['level'];
        $info = M('certificate')->alias('c')->field('user_avatar,user_name,user_sex,user_country,user_nation,user_birthday,certificate_id,certificate_time,certificate_major_id,certificate_level')->join('midi_user as u on c.certificate_user_id = u.user_id')->where($data)->find();
        $this->backToClient(1,'获取证书信息成功',$info);
    }

    /**
     * 10.关于迷笛
     * @author xuan
     */
    public function aboutMIDI(){
        $json_arr = $this->Security();
        $data['article_type'] = 5;
        $article = M('Article')->field('article_id,article_title,article_url,article_pic')->where($data)->select();
        foreach ($article as $k => $v) {
            $article[$k]['article_url'] = 'http://www.xuancode.com/index.php/App/User/article_details/id/'.$v['article_id'];
        }
        $this->backToClient(1,'获取关于迷笛列表成功',$article);
    }

    /**
     * 11.考试历史(分页)
     * @author xuan
     * @param user_id        考官midiID（必传）
     */
    public function examHistory(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 6;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $data['exam_examiner_id'] = $json_arr['user_id'];
        $data['exam_midiclub_id'] = -1;
        $data['exam_paystatus'] = 2;
        $data['exam_status'] = 3;
        $exam_arr = M('exam')->alias('e')->field('exam_id,user_avatar,user_name,user_sex,exam_major_id,exam_level,exam_ymd,examtime_time,exam_comment,exam_result')->join('left join midi_user as u on u.user_id = e.exam_user_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->where($data)->page($p,$pnum)->order('exam_ymd desc')->select();
        $this->backToClient(1,'获取考试历史成功！',$exam_arr);
    }

    /**
     * 12.考试时段查询
     * @author xuan
     */
    public function queryExamtime(){
        $json_arr = $this->Security();
        $examtime_arr = M('examtime')->field('examtime_id,examtime_time,examtime_part')->select();
        $this->backToClient(1,'考试时段查询成功！',$examtime_arr);
    }

    /**
     * 13.考试设置查询
     * @author xuan
     * @param user_id     考官midiID（必传）
     */
    public function queryExamoptions(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 6;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $examoptions = array();
        $data['examoptions_user_id'] = $json_arr['user_id'];
        for ($i=0; $i < 7; $i++) {
            $data['examoptions_ymd'] = date('Y-m-d',strtotime('+'.$i.' days'));
            $examoptions[] = M('examoptions')->field('examoptions_ymd,examoptions_time')->where($data)->order('examoptions_ymd')->find();
        }
        $notallow = '';
        $time_arr = M('examtime')->select();
        foreach ($time_arr as $k => $v) {
            if (date('H:i') > $v['examtime_hi']) {
                if ($notallow == '') {
                    $notallow = $v['examtime_id'];
                }else{
                    $notallow .= ','.$v['examtime_id'];
                }
            }
        }
        //过期的时间段
        if (empty($examoptions[0])) {
        	$examoptions[0]['examoptions_ymd'] = date('Y-m-d');
        	$examoptions[0]['examoptions_time'] = '';
        }
        $examoptions[0]['examoptions_deadline'] = $notallow;
        $examoptions = array_filter($examoptions);
        $examoptions = array_values($examoptions);
        $this->backToClient(1,'考试设置查询成功！',$examoptions);
    }

    /**
     * 14.考试设置保存（七天）
     * @author xuan
     * @param user_id                 考官midiID（必传）
     * @param options                 考试设置（必传）(格式例  "2017-01-09:1,2,3,4;2017-01-10:2,3,4,5,6;2017-01-11:4,5,6,7,8,9,10")
     */
    public function setExamoptions(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 6;
        // $json_arr['options'] = "2016-09-09:1,2,3,4,5,6,7,8;";
        if (empty($json_arr['user_id']) || empty($json_arr['options'])) {
            $this->backToClient(2,'参数不全');
        }
        $data['examoptions_user_id'] = $json_arr['user_id'];
        $options_arr = explode(';',$json_arr['options']);
        foreach ($options_arr as $key => $value) {  //2016-09-09:1,2,3,4,5,6
            $options = explode(':', $value);
            $data['examoptions_ymd'] = $options[0];
            if ($examoptions = M('examoptions')->where($data)->find()) {    //表中有
                $condition1['examoptions_id'] = $examoptions['examoptions_id'];
                $condition1['examoptions_time'] = $options[1];
                M('examoptions')->save($condition1);
            }else{  //表中无，创建一条
                $condition2['examoptions_user_id'] = $json_arr['user_id'];
                $condition2['examoptions_ymd'] = $options[0];
                $condition2['examoptions_time'] = $options[1];
                M('examoptions')->add($condition2);
            }
        }
        $this->backToClient(1,'考试设置成功！');
    }

    /**
     * 15.我的账户列表(分页)
     * @author xuan
     * @param user_id                 考官midiID（必传）
     * @param time_zone               时间段 （1本月 2上个月 3上季度 4上半年）（必传）
     */
    public function myAccount(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 6;
        // $json_arr['time_zone'] = 1;
        if (empty($json_arr['user_id']) || empty($json_arr['time_zone'])) {
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $data['exam_examiner_id'] = $json_arr['user_id'];
        $data['exam_status'] = 3;
        $data['exam_paystatus'] = 2;
        $data['exam_midiclub_id'] = -1;
        $firstday = date('Y-m-01',time());
        switch ($json_arr['time_zone']) {
            case '1':
                $data['exam_ymd'] = array('EGT',$firstday);
                break;
            case '2':
                $data['exam_ymd'] = array(array('EGT',date('Y-m-01',strtotime('-1 months'))),array('LT',$firstday),'AND');
                break;
            case '3':
                $data['exam_ymd'] = array(array('EGT',date('Y-m-01',strtotime('-3 months'))),array('LT',$firstday),'AND');
                break;
            case '4':
                $data['exam_ymd'] = array(array('EGT',date('Y-m-01',strtotime('-6 months'))),array('LT',$firstday),'AND');
                break;
            default:
                $data['exam_ymd'] = array('EGT',$firstday);
                break;
        }
        $exam_arr = M('exam')->alias('e')->field('u.user_name,u.user_avatar,e.exam_income,e.exam_major_id,e.exam_level,e.exam_endtime,e.exam_ymd,t.examtime_hi')->where($data)->join('midi_user as u on e.exam_user_id = u.user_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->page($p,$pnum)->order('exam_ymd desc')->select();
        $this->backToClient(1,'获取我的账户列表成功！',$exam_arr);
    }

    /**
     * 16.我的账户总和
     * @author xuan
     * @param user_id                   考官midiID（必传）
     */
    public function totalAccount(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 6;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $account = array();
        $data['exam_examiner_id'] = $json_arr['user_id'];
        $data['exam_status'] = 3;
        $data['exam_paystatus'] = 2;
        $data['exam_midiclub_id'] = -1;
        $firstday = date('Y-m-01',time());
        //本月
        $data['exam_ymd'] = array('EGT',$firstday);
        $account['this'] = M('exam')->where($data)->sum('exam_income');
        //上个月
        $data['exam_ymd'] = array(array('EGT',date('Y-m-01',strtotime('-1 months'))),array('LT',$firstday),'AND');
        $account['one'] = M('exam')->where($data)->sum('exam_income');
        //上季度
        $data['exam_ymd'] = array(array('EGT',date('Y-m-01',strtotime('-3 months'))),array('LT',$firstday),'AND');
        $account['three'] = M('exam')->where($data)->sum('exam_income');
        //上半年
        $data['exam_ymd'] = array(array('EGT',date('Y-m-01',strtotime('-6 months'))),array('LT',$firstday),'AND');
        $account['six'] = M('exam')->where($data)->sum('exam_income');
        foreach ($account as $key => $value) {
            if (empty($value)) {
                $account[$key] = 0.00;
            }
        }
        $this->backToClient(1,'获取账户总额成功！',$account);
    }

    /**
     * 17.完成考试
     * @author xuan
     * @param exam_id               考试ID（必传）
     * @param exam_result           考试结果 2考试通过 3考试未通过（必传）
     */
    public function finishExam(){
        $json_arr = $this->Security();
        // $json_arr['exam_id'] = 36;
        // $json_arr['exam_result'] = 2;
        if (empty($json_arr['exam_id']) || empty($json_arr['exam_result'])) {
            $this->backToClient(2,'参数不全');
        }
        //考试结束时间
        $json_arr['exam_endtime'] = time();
        //考试状态改为已完成考试
        $json_arr['exam_status'] = 3;
        M('exam')->save($json_arr);
        $exam_arr = M('exam')->where('exam_id = '.$json_arr['exam_id'])->find();
        //给考生发推送，告知分数出来
        $user_value = array('tag'=>array('user'.$exam_arr['exam_user_id']));
        push($user_value,'迷笛考级提醒您：您有一场考试结果已出，请查看！',C(JPUSH_USER),5,$json_arr['exam_id']);
        //给考生发短信
        $user_phone = M('user')->where('user_id = '.$exam_arr['exam_user_id'])->getField('user_phone');
        alidayumsg($user_phone,'','SMS_80370035');
        // //考官点评成功之后，推送给考官，告知佣金已到
        // $examiner_value = array('tag'=>array('examiner'.$exam_arr['exam_examiner_id']));
        // push($examiner_value,'考试已结束，您的账单已加钱',C(JPUSH_EXAMINER));
        //如果考试通过，则更新当前考生的专业等级
        if ($exam_arr['exam_result'] == 2) {
            if ($exam_arr['exam_major_id'] == 1) {
                $data['user_level_guitar'] = $exam_arr['exam_level'];
            }elseif ($exam_arr['exam_major_id'] == 2) {
                $data['user_level_drum'] = $exam_arr['exam_level'];
            }elseif ($exam_arr['exam_major_id'] == 3) {
                $data['user_level_bass'] = $exam_arr['exam_level'];
            }
            $data['user_id'] = $exam_arr['exam_user_id'];
            M('user')->save($data);
        }
        $this->backToClient(1,'考试完成');
    }

    /**
     * 18.考官点评
     * @author xuan
     * @param exam_id            考试id（必填）
     * @param exam_comment       评论内容（必填）
     */
    public function examComment(){
        $json_arr = $this->Security();
        if (empty($json_arr['exam_id']) || empty($json_arr['exam_comment'])) {
            $this->backToClient(2,'参数不全');
        }
        if (M('exam')->save($json_arr)) {
            $condition['exam_id'] = $json_arr['exam_id'];
            $user = M('exam')->field('exam_user_id')->where($condition)->find();
            $user_value = array('tag'=>array('user'.$user['exam_user_id']));
            push($user_value,'迷笛考级提醒您：您有一场考试被考官点评，请查看！',C(JPUSH_USER),6,$condition['exam_id']);
            $this->backToClient(1,'考官点评成功！');
        }else{
            $this->backToClient(3,'考官点评失败！');
        }
    }

    /**
     * 19.考官创建房间（作废2017-08-01）
     * @author xuan
     * @param exam_id            考试id（必填）
     * @param exam_room_id       考官创建的房间id（必填）
     */
    // public function createRoom(){
    //     $json_arr = $this->Security();
    //     if (empty($json_arr['exam_id']) || empty($json_arr['exam_room_id'])) {
    //         $this->backToClient(2,'参数不全');
    //     }
    //     $data['exam_id'] = $json_arr['exam_id'];
    //     $exam_arr = M('exam')->where($data)->find();
    //     if (!empty($exam_arr['exam_room_id'])) {
    //         $this->backToClient(4,'房间之前已经创建过了');
    //     }
    //     if (M('exam')->save($json_arr)) {
    //         //考官创建房间成功，推送通知给考生
    //         $user_value = array('tag'=>array('user'.$exam_arr['exam_user_id']));
    //         push($user_value,'考官创建房间成功，请做好考试准备',C(JPUSH_USER),3,$data['exam_id']);
    //         //添加一条消息
    //         $message['message_user_id'] = $exam_arr['exam_user_id'];
    //         $message['message_client'] = 1;
    //         $message['message_type'] = 3;
    //         $message['message_title'] = '考官创建房间成功，请做好考试准备';
    //         $message['message_content'] = '考官创建房间成功，请做好考试准备';
    //         $message['message_extra'] = $json_arr['exam_room_id'];
    //         $message['message_time'] = time();
    //         M('message')->add($message);

    //         $this->backToClient(1,'考官创建房间成功！');
    //     }else{
    //         $this->backToClient(3,'考官创建房间失败！');
    //     }
    // }

    /**
     * 21.考官个人信息
     * @author xuan
     * @param id midiID
     */
    public function examinerInfo(){
    	$json_arr = $this->Security();
    	// $json_arr['id'] = 6;
    	if (empty($json_arr['id'])) {
    		$this->backToClient(2,'参数不全');
    	}
        $data['user_id'] = $json_arr['id'];
        $data['user_display'] = 1;
        $user = M('user')->where($data)->find();
        unset($user['user_password']);
        unset($user['user_income_proportion']);
        $this->backToClient(1,'获取考官信息成功',$user);
    }

    /**
     * 22.获取消息详情
     * @author xuan
     * @param id 消息id
     */
    public function messageInfo(){
    	$json_arr = $this->Security();
    	// $json_arr['id'] = 6;
    	if (empty($json_arr['id'])) {
    		$this->backToClient(2,'参数不全');
    	}
        $data['message_id'] = $json_arr['id'];
        $data['message_display'] = 1;
        $message = M('message')->where($data)->find();
        $this->backToClient(1,'获取消息详情成功',$message);
    }

    /**
     * 23.获取考试详情
     * @author xuan
     * @param id 考试id
     */
    public function examInfo(){
    	$json_arr = $this->Security();
    	// $json_arr['id'] = 48;
    	if (empty($json_arr['id'])) {
    		$this->backToClient(2,'参数不全');
    	}
    	$data['exam_id'] = $json_arr['id'];
        $exam_arr = M('exam')->alias('e')->field('exam_id,user_avatar,user_name,user_sex,exam_major_id,exam_level,exam_ymd,exam_comment,exam_result,user_idpic1,examtime_time')->join('left join midi_user as u on u.user_id = e.exam_user_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->where($data)->find();
        $this->backToClient(1,'获取考试详情成功！',$exam_arr);
    }

    /**
     * 获取录像列表（分页）
     * @author xuan
     * @param exam_id           考试id(必传)
     */
    public function getVodList(){
        $json_arr = $this->Security();
        // $json_arr['exam_id'] = 40;
        if (empty($json_arr['exam_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $video_arr = array();
        $vod = getVod();
        $package['fileName'] = $json_arr['exam_id'];
        $package['pageNo'] = (!empty($json_arr['p']))?$json_arr['p']:1;
        $package['pageSize'] = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $vod_arr = $vod->DescribeVodPlayInfo($package);
        foreach ($vod_arr['fileSet'] as $k => $v) {
            if ($v['status'] == 2) {
                $arr = array();
                $arr['fileName'] = $v['fileName'];
                $arr['imageUrl'] = $v['imageUrl'];
                $arr['filePath'] = $v['playSet'][2]['url'];
                $video_arr[] = $arr;
            }
        }
        $this->backToClient(1,'获取录像列表成功',$video_arr);
    }

    //============================= 机构端 ====================================

    /**
     * 机构考试设置保存
     * @author xuan
     * @param user_id           考官midiID（必传）
     * @param options           考试设置（必传）(格式例 "2017-01-09:1;2017-01-10:3;2017-01-11:2")
     */
    public function setOrgExamOptions(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 100;
        // $json_arr['options'] = "2018-03-09:1;2018-03-22:3;2018-03-23:3";
        if (empty($json_arr['user_id']) || empty($json_arr['options'])) {
            $this->backToClient(2,'参数不全');
        }
        $user_arr = M('user')->where(['user_id'=>$json_arr['user_id']])->find();
        if (empty($user_arr)) {
            $this->backToClient(3,'用户不存在');
        }else{
            if ($user_arr['user_display'] == 0) {
                $this->backToClient(4,'账号被禁用');
            }elseif (!in_array($user_arr['user_role'],[3,4])) {
                $this->backToClient(5,'非考官身份');
            }
        }
        $options_arr = explode(';',$json_arr['options']);
        foreach ($options_arr as $k => $v) {
            $options = explode(':',$v);
            $condition = [];
            $condition['examorg_options_userid'] = $user_arr['user_id'];
            $condition['examorg_options_date'] = $options[0];
            $condition['examorg_options_time'] = $options[1];
            if (M('examorg_options')->where($condition)->find()) {
                continue;
            }
            $condition['examorg_options_guitar'] = $user_arr['user_examiner_guitar'];
            $condition['examorg_options_drum'] = $user_arr['user_examiner_drum'];
            $condition['examorg_options_bass'] = $user_arr['user_examiner_bass'];
            $condition['examorg_options_allowtime'] = $options[1];
            M('examorg_options')->add($condition);
        }
        $this->backToClient(1,'机构考试设置保存成功');
    }

    /**
     * 机构考试设置查询
     * @author xuan
     * @param user_id           考官midiID（必传）
     */
    public function queryOrgExamOptions(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 100;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        //设置时间为两周
        $start_time = date('Y-m-d',time());
        $end_time = date('Y-m-d',time()+14*24*60*60);
        $condition['examorg_options_userid'] = $json_arr['user_id'];
        $condition['examorg_options_date'] = ['BETWEEN',[$start_time,$end_time]];
        $examorg_options_arr = M('examorg_options')->field('examorg_options_date,examorg_options_time')->where($condition)->order('examorg_options_date asc')->select();
        $this->backToClient(1,'机构考试设置查询成功',$examorg_options_arr);
    }

    /**
     * 获取机构考试日程
     * @author xuan
     * @param user_id           考官midiID（必传）
     * @param date              考试日期（格式Y-m-d）（必填）
     */
    public function orgExamSchedule(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 100;
        // $json_arr['date'] = '2018-3-18';
        if (empty($json_arr['user_id']) || empty($json_arr['date'])) {
            $this->backToClient(2,'参数不全');
        }
        $condition['examorg_examinerid'] = $json_arr['user_id'];
        $condition['examorg_date'] = $json_arr['date'];
        $condition['examorg_status'] = 1;
        $examorg_arr = M('examorg')->field('m.midiclub_name,m.midiclub_pic,e.examorg_id,e.examorg_orgid,e.examorg_date,e.examorg_time,e.examorg_guitar,e.examorg_drum,e.examorg_bass,e.examorg_enrollnum,e.examorg_maxnum,e.examorg_roomid')->alias('e')->join('left join midi_midiclub as m on e.examorg_orgid = m.midiclub_id')->where($condition)->order('examorg_time asc')->select();
        $this->backToClient(1,'获取机构考试日程成功',$examorg_arr);
    }

    /**
     * 获取机构考试详情
     * @author xuan
     * @param examorg_id           机构考试ID（必填）
     */
    public function orgExamDetails(){
        $json_arr = $this->Security();
        // $json_arr['examorg_id'] = 24;
        if (empty($json_arr['examorg_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $examorg_arr = M('examorg')->alias('e')->field('m.midiclub_name,m.midiclub_pic,e.examorg_date,e.examorg_time,e.examorg_guitar,e.examorg_drum,e.examorg_bass,e.examorg_enrollnum,e.examorg_maxnum')->join('left join midi_midiclub as m on e.examorg_orgid = m.midiclub_id')->where(['examorg_id'=>$json_arr['examorg_id']])->find();
        $this->backToClient(1,'获取机构考试详情成功！',$examorg_arr);
    }

    /**
     * 获取机构考试学生列表（分页）
     * @author xuan
     * @param examorgid           机构考试id（必填）
     */
    public function orgExamStudentList(){
        $json_arr = $this->Security();
        // $json_arr['examorgid'] = 24;
        if (empty($json_arr['examorgid'])) {
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $condition['examorg_enroll_examorgid'] = $json_arr['examorgid'];
        $condition['examorg_enroll_status'] = ['IN','2,3'];
        $examorg_enroll_arr = M('examorg_enroll')->alias('e')->field('e.examorg_enroll_id,e.examorg_enroll_avatar,e.examorg_enroll_major,e.examorg_enroll_level,e.examorg_enroll_date,e.examorg_enroll_result,e.examorg_enroll_mark,e.examorg_enroll_comment,u.user_id,u.user_name,u.user_idcard')->join('left join midi_user as u on e.examorg_enroll_userid = u.user_id')->where($condition)->order('examorg_enroll_status asc')->page($p,$pnum)->select();
        $this->backToClient(1,'获取机构考试学生列表成功',$examorg_enroll_arr);
    }

    /**
     * 获取机构考试考生详情
     * @author xuan
     * @param user_id           考生id（必填）
     */
    public function studentDetails(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 5836;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $user_arr = M('user')->field('user_id,user_avatar,user_name,user_nickname,user_level_guitar,user_level_drum,user_level_bass,user_sex,user_nation,user_birthday,user_phone,user_email,user_idcard,user_idpic1')->where(['user_id'=>$json_arr['user_id']])->find();
        $this->backToClient(1,'获取机构考试考生详情成功',$user_arr);
    }

    /**
     * 获取考试历史列表（分页）
     * @author xuan
     * @param user_id           考官midiID（必传）
     */
    public function orgExamHistoryList(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 100;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $condition['examorg_examinerid'] = $json_arr['user_id'];
        $condition['examorg_status'] = 2;
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $examorg_arr = M('examorg')->alias('e')->field('m.midiclub_name,m.midiclub_pic,e.examorg_id,e.examorg_date,e.examorg_time,e.examorg_guitar,e.examorg_drum,e.examorg_bass,e.examorg_enrollnum,e.examorg_maxnum')->join('left join midi_midiclub as m on e.examorg_orgid = m.midiclub_id')->where($condition)->order('e.examorg_date desc,e.examorg_time asc')->page($p,$pnum)->select();
        $this->backToClient(1,'获取考试历史列表成功',$examorg_arr);
    }

    /**
     * 给机构考试的考生打分数
     * @author xuan
     * @param examorg_enroll_id                 考试ID（必传）
     * @param examorg_enroll_result             考试结果 2考试通过 3考试未通过（必传）
     */
    public function markOrgExam(){
        $json_arr = $this->Security();
        // $json_arr['examorg_enroll_id'] = 9;
        // $json_arr['examorg_enroll_result'] = 2;
        if (empty($json_arr['examorg_enroll_id']) || empty($json_arr['examorg_enroll_result'])) {
            $this->backToClient(2,'参数不全');
        }
        $condition['examorg_enroll_id'] = $json_arr['examorg_enroll_id'];
        $examorg_enroll_arr = M('examorg_enroll')->where($condition)->find();
        if (empty($examorg_enroll_arr)) {
            $this->backToClient(3,'考试不存在');
        }elseif($examorg_enroll_arr['examorg_enroll_status'] != 3){
            $this->backToClient(4,'考试未结束，不能打分');
        }elseif ($examorg_enroll_arr['examorg_enroll_result'] != 1) {
            $this->backToClient(5,'结果已出，不能重复打分');
        }elseif (!in_array($json_arr['examorg_enroll_result'], [2,3])) {
            $this->backToClient(6,'考试结果参数错误');
        }
        $condition['examorg_enroll_result'] = $json_arr['examorg_enroll_result'];
        M('examorg_enroll')->save($condition);
        $this->backToClient(1,'给机构考试的考生打分数完成！');
    }

    /**
     * 考官点评和标记
     * @author xuan
     * @param examorg_enroll_id                 考试ID（必传）
     * @param examorg_enroll_mark               标记内容（选填）
     * @param examorg_enroll_comment            考试评语（选填）
     */
    public function orgExamComment(){
        $json_arr = $this->Security();
        // $json_arr['examorg_enroll_id'] = 9;
        if (empty($json_arr['examorg_enroll_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $condition['examorg_enroll_id'] = $json_arr['examorg_enroll_id'];
        $examorg_enroll_arr = M('examorg_enroll')->where($condition)->find();
        if (empty($examorg_enroll_arr)) {
            $this->backToClient(3,'考试不存在');
        }
        if (!empty($json_arr['examorg_enroll_mark'])) {
            $condition['examorg_enroll_mark'] = $json_arr['examorg_enroll_mark'];
        }
        if (!empty($json_arr['examorg_enroll_comment'])) {
            $condition['examorg_enroll_comment'] = $json_arr['examorg_enroll_comment'];
        }
        M('examorg_enroll')->save($condition);
        $this->backToClient(1,'考官点评成功！');
    }

    /**
     * 获取机构考试学生详情
     * @author xuan
     * @param examorg_enroll_id             考试报名id（必填）
     */
    public function orgExamStudentDetails(){
        $json_arr = $this->Security();
        // $json_arr['examorg_enroll_id'] = 9;
        if (empty($json_arr['examorg_enroll_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $condition['examorg_enroll_id'] = $json_arr['examorg_enroll_id'];
        $examorg_enroll_arr = M('examorg_enroll')->field('e.examorg_enroll_id,e.examorg_enroll_avatar,e.examorg_enroll_major,e.examorg_enroll_level,e.examorg_enroll_date,examorg_enroll_audio1,examorg_enroll_audio2,e.examorg_enroll_result,e.examorg_enroll_mark,e.examorg_enroll_comment,u.user_id,u.user_name,u.user_sex,u.user_idcard,u.user_idpic1')->alias('e')->join('left join midi_user as u on e.examorg_enroll_userid = u.user_id')->where($condition)->find();
        $this->backtoclient(1,'获取机构考试学生详情成功',$examorg_enroll_arr);
    }

    /**
     * 获取测试房间列表（分页）
     * @author xuan
     * @param org_name           机构名称（选传）
     */
    public function getTestingRoomList(){
        $json_arr = $this->Security();
        // $json_arr['org_name'] = 100;
        $condition['midiclub_display'] = 1;
        $condition['midiclub_testing'] = 2;
        if (!empty($json_arr['org_name'])) {
            $condition['midiclub_name'] = array('LIKE',"%{$json_arr['org_name']}%");
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $midiclub_arr = M('midiclub')->field('midiclub_id,midiclub_name,midiclub_address,midiclub_pic')->where($condition)->page($p,$pnum)->select();
        $this->backtoclient(1,'获取测试房间列表成功',$midiclub_arr);
    }







}